<?php
global_config::get_instance()->add_server_class("SoapAlphabetOfChains","soap_alphabet_chains");
require_once(dirname(__FILE__)."/soap_alphabet_base.class.php");
class soap_alphabet_chains extends soap_alphabet_base
{
	function __construct() {
		$this->Elements = new soap_array_phantomessage_chains();
	}

	public function load_from_db($alphabet_id)
	{
		$this->Alphabet_id = $alphabet_id;
		$dba = db_adapter::get_instance();
		$alphabet_phantommessage = new  alphabet_phantommessage();
		$criteria = new query_criteria();
		$criteria->add_criterion($alphabet_phantommessage->alphabet_id,criterion_type::$equal,$alphabet_id);
		$alphabet_list = $dba->get_list($alphabet_phantommessage,$criteria, $total, null, null, $alphabet_phantommessage->get_table_name().".".$alphabet_phantommessage->position->get_field_name(),"asc");
		while ($alphabet = $alphabet_list->get_next(false, true))
		{
			$temp = new soap_pmessage();
			$temp->load_from_db($alphabet->alphabet_phantommessage->phantommessage_id->get_value());
			$this->Elements->add($temp);
		}

	}

	/**
	 * Get id of alphabet contains this elements
	 *
	 * @param array $phantom_messages_ids
	 * @return int
	 */
	protected function get_alphabet_id_equals_this($phantom_messages_ids)
	{
		$dba = db_adapter::get_instance();
		$array_cross = array();
		$i = 0;
		do
		{
			$array_buf = array();

			$alphabet_phantommessage = new alphabet_phantommessage();

			$criteria = new query_criteria();
			$criteria->add_criterion($alphabet_phantommessage->phantommessage_id , criterion_type::$equal, $phantom_messages_ids[$i]);
			$criteria->add_criterion($alphabet_phantommessage->position, criterion_type::$equal,$i);
			$alphabet_phantommessage_list = $dba->get_list($alphabet_phantommessage,$criteria);

			while($item = $alphabet_phantommessage_list->get_next()) {
				$array_buf[]=$item->alphabet_id->get_value();
			}			

			$array_cross = !empty($array_cross)? array_intersect($array_cross, $array_buf) : $array_cross = $array_buf;

			$i++;
		}while(!empty($array_cross) && $i<count($phantom_messages_ids));

        if (count($this->Elements->as_array()) > 1)
        {
                var_dump($array_cross);
        }
		

		foreach ($array_cross as $alph_id) 
		{

			$alphabet_cross = new alphabet_phantommessage();

			$criteria = new query_criteria();
			$criteria->add_criterion($alphabet_phantommessage->alphabet_id , criterion_type::$equal, $alph_id);
			$alphabet_cross_list = $dba->get_list($alphabet_cross,$criteria);

			if (count($phantom_messages_ids) == $alphabet_cross_list->count())
			{
				return $alph_id;
			}
		}
		return '';
	}

	/**
	 * Save messages to alphabet
	 *
	 * @param int $alphabet_id
	 * @param array $phantom_messages_ids
	 */
	protected function save_message_to_alphabet($alphabet_id , $phantom_messages_ids)
	{
		if (empty($alphabet_id))
		{
			throw new Exception("Try to save ". var_dump($phantom_messages_ids)."to - $alphabet_id - aphabet" );
		}
		$dba = db_adapter::get_instance();
		$pos = 0;
		foreach($phantom_messages_ids as $mess_id){
			$alphabet_phantommessage = new alphabet_phantommessage();
			$alphabet_phantommessage->alphabet_id->set_value($alphabet_id);
			$alphabet_phantommessage->phantommessage_id->set_value($mess_id);
			$alphabet_phantommessage->position->set_value($pos);
			$dba->save_unique($alphabet_phantommessage);
			$pos++;
		}
	}

	/**
	 * Create new Alphabet in DB
	 *
	 * @return int created alphabet
	 */
	protected function save_new_alphabet()
	{
		return parent::save_new_alphabet('chain');
	}
}
?>
